package com.agile.major.web.rest;

import com.agile.major.domain.RecruitStudent;
import com.agile.major.service.RecruitStudentService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing RecruitStudent.
 */
@RestController
@RequestMapping("/api")
public class RecruitStudentResource {

    private final Logger log = LoggerFactory.getLogger(RecruitStudentResource.class);

    private static final String ENTITY_NAME = "recruitStudent";

    private final RecruitStudentService recruitStudentService;

    public RecruitStudentResource(RecruitStudentService recruitStudentService) {
        this.recruitStudentService = recruitStudentService;
    }

    /**
     * POST  /recruit-students : Create a new recruitStudent.
     *
     * @param recruitStudent the recruitStudent to create
     * @return the ResponseEntity with status 201 (Created) and with body the new recruitStudent, or with status 400 (Bad Request) if the recruitStudent has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/recruit-students")
    @Timed
    public ResponseEntity<RecruitStudent> createRecruitStudent(@RequestBody RecruitStudent recruitStudent) throws URISyntaxException {
        log.debug("REST request to save RecruitStudent : {}", recruitStudent);
        if (recruitStudent.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new recruitStudent cannot already have an ID")).body(null);
        }
        RecruitStudent result = recruitStudentService.save(recruitStudent);
        return ResponseEntity.created(new URI("/api/recruit-students/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /recruit-students : Updates an existing recruitStudent.
     *
     * @param recruitStudent the recruitStudent to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated recruitStudent,
     * or with status 400 (Bad Request) if the recruitStudent is not valid,
     * or with status 500 (Internal Server Error) if the recruitStudent couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/recruit-students")
    @Timed
    public ResponseEntity<RecruitStudent> updateRecruitStudent(@RequestBody RecruitStudent recruitStudent) throws URISyntaxException {
        log.debug("REST request to update RecruitStudent : {}", recruitStudent);
        if (recruitStudent.getId() == null) {
            return createRecruitStudent(recruitStudent);
        }
        RecruitStudent result = recruitStudentService.save(recruitStudent);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, recruitStudent.getId().toString()))
            .body(result);
    }

    /**
     * GET  /recruit-students : get all the recruitStudents.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of recruitStudents in body
     */
    @GetMapping("/recruit-students")
    @Timed
    public ResponseEntity<Page<RecruitStudent>> getAllRecruitStudents(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of RecruitStudents");
        Page<RecruitStudent> page = recruitStudentService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/recruit-students");
        return new ResponseEntity<>(page, headers, HttpStatus.OK);
    }
    /**
     * GET  /recruit-students/:id : get the "id" recruitStudent.
     *
     * @param id the id of the recruitStudent to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the recruitStudent, or with status 404 (Not Found)
     */
    @GetMapping("/recruit-students/{id}")
    @Timed
    public ResponseEntity<RecruitStudent> getRecruitStudent(@PathVariable Long id) {
        log.debug("REST request to get RecruitStudent : {}", id);
        RecruitStudent recruitStudent = recruitStudentService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(recruitStudent));
    }

    /**
     * DELETE  /recruit-students/:id : delete the "id" recruitStudent.
     *
     * @param id the id of the recruitStudent to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/recruit-students/{id}")
    @Timed
    public ResponseEntity<Void> deleteRecruitStudent(@PathVariable Long id) {
        log.debug("REST request to delete RecruitStudent : {}", id);
        recruitStudentService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    @GetMapping("/recruit-students/{id}/{year}")
    @Timed
    public ResponseEntity<List<RecruitStudent>> getRecruitStuMap(@PathVariable Long id,@PathVariable String year) {
        List<RecruitStudent> recruitStudent = recruitStudentService.getRecruitStuByMajorAndYear(id,year);
        return new ResponseEntity<>(recruitStudent, HttpStatus.OK);

    }
}
